#!/bin/bash
#description: 启动重启server服务
#端口号，根据此端口号确定PID

PORT="9001"
STARTCONFIG="-Xmx500m -Xms100m -Xss256k"

#查询出监听了PORT端口TCP协议的程序
pid=`netstat -anp|grep ${PORT}|awk '{printf $7}'|cut -d/ -f1`

# 服务启动
doStart(){
	# 检查是否已经启动
	if [ -n "${pid}" ]; then  
		echo "Server already start, pid:${pid}"
		
		return 0
	fi
	
	# 启动java应用
	nohup java ${STARTCONFIG} -classpath ".:./classes:./lib/*" cn.virens.App &
	
	echo "Start at port:${PORT}"  
}

# 服务停止
doStop(){  
   if [ -z "${pid}" ]; then  
      echo "Not find program on port:${PORT}"  
      return 0
   fi
   
   #结束程序
   kill -9 ${pid}
   rm -rf ${pid}
   echo "Kill program use signal 2,pid:${pid}"  
}

#状态查询
doStatus(){  
   if [ -z "${pid}" ]; then  
      echo "Not find program on port:${PORT}"  
   else  
      echo "Program is running,pid:${pid}"  
   fi  
}  

# 启动参数
case $1 in  
   start)  
      doStart  
   ;;  
   stop)  
      doStop  
   ;;
   status)  
      doStatus  
   ;;  
   *)  
      echo "Usage: {start|stop|status}"  
   ;;  
esac  
  
exit 0